package com.springboot.original.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 获取系统中的Spring ApplicationContext 对象
 *
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {

    protected static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static ApplicationContext getContext() {
        return context;
    }

    public static <T> T getBean(Class<T> classz){
        return getContext().getBean(classz);
    }

    public static Object getBeanByName(String name){
        return getContext().getBean(name);
    }
}
